package com.jf.blog_jf04.controller;

import com.jf.blog_jf04.bean.Category;
import com.jf.blog_jf04.data.PageInfo;
import com.jf.blog_jf04.data.dto.CategoryDto;
import com.jf.blog_jf04.data.ov.CategorySearchOV;
import com.jf.blog_jf04.data.ov.CategoryUpdateOV;
import com.jf.blog_jf04.service.CategoryService;
import com.jf.blog_jf04.utils.BeanConvertUtils;
import com.jf.blog_jf04.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/category")
@RestController
@Api(tags = "分类模块")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @GetMapping("/page")
    @ApiOperation("分页筛选")
    public R page(PageInfo pageInfo, CategorySearchOV ov) {
        return categoryService.getPage(pageInfo, ov);
    }

    @GetMapping("/info/{categoryId}")
    @ApiOperation("详细信息")
    public R info(@ApiParam("分类编号") @PathVariable int categoryId) {
        Category category = categoryService.getById(categoryId);
        return R.success(BeanConvertUtils.copyBean(category, CategoryDto.class));
    }

    @PostMapping("/update")
    @ApiOperation("添加或修改")
    public R update(@RequestBody CategoryUpdateOV ov) {
        categoryService.saveOrUpdate(BeanConvertUtils.copyBean(ov, Category.class));
        return R.success();
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除数据")
    public R delete(@ApiParam("批量删除id") @RequestParam List<Integer> ids) {
        return categoryService.delete(ids);
    }

    @GetMapping("/select/{type}")
    @ApiOperation("下拉框数据")
    public R select(@ApiParam(value = "获取数据的类型1全部2当前", allowableValues = "1,2", defaultValue = "1", example = "1") @PathVariable int type) {
        return categoryService.getSelect(type);
    }
}
